"""
Case Type   : USTORE
Case Name   : 验证参数enable_recyclebin的生效机制
Create At   : 2025/3/10
Owner       : wan005
Description :
    1.查询参数默认值
    2.创建astore表和ustore表
    3.truncate和drop表后查看回收站
    4.修改参数值为on，truncate和drop表后查看回收站
Expect      :
    1.参数默认值off
    2.创建成功
    3.两次查询均无数据
    4.第一次查询无数据，第二次查询有数据
History     :
"""
import os
import unittest
import sys
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table01 = 't_guc_01'
        self.table02 = 't_guc_02'

    def test_ustore(self):
        text = '-----step1:查询参数默认值;expect:参数默认值off-----'
        self.logger.info(text)
        res1 = self.common.show_param('enable_recyclebin')
        self.assertEqual('off', res1, text + '执行失败')

        text = '-----step2:创建astore表和ustore表;expect:创建成功-----'
        self.logger.info(text)
        sql_create = f'create table {self.table01}(a int primary key) ' \
                   f'with(storage_type=astore);' \
                   f'create table {self.table02}(a int primary key) ' \
                   f'with(storage_type=ustore);'
        res2 = self.sh_primy.execut_db_sql(sql_create)
        self.logger.info(res2)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res2, text + '执行失败')

        text = '-----step3:truncate和drop表后查看回收站;expect:两次查询均无数据-----'
        self.logger.info(text)
        sql_trun1 = f'truncate table {self.table01};drop table {self.table01};'
        res3_trun1 = self.sh_primy.execut_db_sql(sql_trun1)
        self.logger.info(res3_trun1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res3_trun1,
                      text + '执行失败')
        sql_select = 'select rcyoriginname from gs_recyclebin;'
        res_select1 = self.sh_primy.execut_db_sql(sql_select)
        self.logger.info(res_select1)
        self.common.equal_sql_mdg(res_select1, 'rcyoriginname', '(0 rows)',
                                  flag='1')
        sql_trun2 = f'truncate table {self.table02};drop table {self.table02};'
        res3_trun2 = self.sh_primy.execut_db_sql(sql_trun2)
        self.logger.info(res3_trun2)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res3_trun2,
                      text + '执行失败')
        res_select2 = self.sh_primy.execut_db_sql(sql_select)
        self.logger.info(res_select2)
        self.common.equal_sql_mdg(res_select2, 'rcyoriginname', '(0 rows)',
                                  flag='1')

        text = '----step4:修改参数值为on，truncate和drop表后查看回收站;expect:成功----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('reload',
                    self.constant.GSGUC_SUCCESS_MSG, 'enable_recyclebin=on')
        res4_1 = self.sh_primy.execut_db_sql(sql_create)
        self.logger.info(res4_1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res4_1,
                      text + '执行失败')
        res4_trun1 = self.sh_primy.execut_db_sql(sql_trun1)
        self.logger.info(res4_trun1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res4_trun1,
                      text + '执行失败')
        res4_select1 = self.sh_primy.execut_db_sql(sql_select)
        self.common.equal_sql_mdg(res4_select1, 'rcyoriginname', '(0 rows)',
                                  flag='1')
        res4_trun2 = self.sh_primy.execut_db_sql(sql_trun2)
        self.logger.info(res4_trun2)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res4_trun2,
                      text + '执行失败')
        res4_select2 = self.sh_primy.execut_db_sql(sql_select)
        self.logger.info(res4_select2)
        self.common.equal_sql_mdg(res4_select2, 'rcyoriginname', 't_guc_02',
                                  't_guc_02_pkey', 't_guc_02',
                                  't_guc_02_pkey', '(4 rows)', flag='1')

    def tearDown(self):
        text = '-----step1:清理环境，恢复配置;expect:成功-----'
        self.logger.info(text)
        clean_sql = f'drop table if exists {self.table01};' \
                     f'drop table if exists {self.table02};'
        clean_msg = self.sh_primy.execut_db_sql(clean_sql)
        self.logger.info(clean_msg)
        self.assertEqual(clean_msg.count('DROP TABLE'), 2, text + '执行失败')
        purge_sql = f'purge recyclebin;'
        purge_msg = self.sh_primy.execut_db_sql(purge_sql)
        self.logger.info(purge_msg)
        self.assertEqual('PURGE RECYCLEBIN', purge_msg, text + '执行失败')
        self.sh_primy.execute_gsguc('reload',
                                    self.constant.GSGUC_SUCCESS_MSG,
                                    'enable_recyclebin=off')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
